﻿#include "QSettingButtonBase.h"
#include "CAppUiConfig.h"
#include "QDevTypeUnified.h"
#include "QUnits.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QKeyEvent>
QSettingButtonBase::QSettingButtonBase(Q_KeyNumber key,QWidgetBase *parent)
: QWidgetBase(parent), m_keyNumber(key)
{
	this->setStyleSheet("background-color:black");
	initUI("", "", "", "");
}

QSettingButtonBase::QSettingButtonBase(Q_KeyNumber key,QString icon, QString num_icon, QString tips1,QString tips2, QWidgetBase *parent)
: QWidgetBase(parent), m_keyNumber(key)
{
	this->setStyleSheet("background-color:black");

	initUI(icon, num_icon, tips1, tips2);
	
}

QSettingButtonBase::~QSettingButtonBase()
{
	
}

void QSettingButtonBase::createUI()
{

}

void QSettingButtonBase::initUI(QString icon,QString num_icon, QString tips1,QString tips2)
{

	setAttribute(Qt::WA_AcceptTouchEvents,true); //接受所有触摸事件
//	m_pQWidget = new QWidget(this);
//	m_pQWidget->resize(width(),height());
	QFont ft;
	ft.setItalic(false);//设置斜体
	//图标显示label
    icon_label = new QGroupLabel(icon, Qt::AlignCenter, Qt::KeepAspectRatio, EOrientation::Image,this);

    if(!tips2.isEmpty()){
    	//设置字体大小
    	ft.setPixelSize(SETTING_WIDGET_FRAME_FONTSIZE0);
    	//提示信息label 比如 本机信息
    	tips_label_1 = new QGroupLabel(tips1, ft,Qt::AlignLeft, Qt::white,this);
    	//设置字体大小
    	ft.setPixelSize(SETTING_WIDGET_FRAME_FONTSIZE1);
    	//提示信息label 比如 请按1号键查看
    	tips_label_2 = new QGroupLabel(tips2, ft,Qt::AlignLeft, Qt::white,this);
    }else{
    	//设置字体大小
    	ft.setPixelSize(FONT_SIZE_LEVEL_2);
    	//提示信息label 比如 本机信息
    	tips_label_1 = new QGroupLabel(tips1, ft,Qt::AlignLeft, Qt::white,true, this);
    }

    if(!num_icon.isEmpty()){
    	//数字图标显示label
    	num_icon_label = new QGroupLabel(num_icon, Qt::AlignCenter, Qt::KeepAspectRatio, EOrientation::Image,this);
    }
	


	QVBoxLayout *vlayout1 = new QVBoxLayout;//创建一个垂直布局管理器
	vlayout1->addStretch(1);    //在label5前面的空白占总空白的1/2
	vlayout1->addWidget(tips_label_1);
	if(tips_label_2)
		vlayout1->addWidget(tips_label_2);
	vlayout1->addStretch(1);    //在label5前面的空白占总空白的1/2
	// 去除布局管理器之间间隔
	vlayout1->setSpacing(0);
	vlayout1->setMargin(0);
	
	QHBoxLayout *hlayout = new QHBoxLayout(this);//创建一个水平布局管理器
	if(icon_label)
		hlayout->addWidget(icon_label,1);
	hlayout->addLayout(vlayout1,1);
	hlayout->addStretch(1);    //在label5前面的空白占总空白的1/1
	if(num_icon_label)
		hlayout->addWidget(num_icon_label,1);
    hlayout->setMargin(0);// 去除边框
    hlayout->setSpacing(0);// 去除间隔
	this->setLayout(hlayout);//将水平布局器加入主窗口
	
}

//初始化信号关联
void QSettingButtonBase::initConnect()
{
	
}

//设置图片和显示文字
void QSettingButtonBase::SetData(QString icon,QString num_icon, QString tips1,QString tips2)
{
	if(!icon.isEmpty()){
		icon_label->SetPixmap(icon);
	}
	if(!num_icon.isEmpty()){
		if(num_icon_label)
			num_icon_label->SetPixmap(num_icon);
	}
	if(!tips1.isEmpty()){
		tips_label_1->SetQStringText(tips1);
	}
	if(!tips2.isEmpty()){
		if(tips_label_2)
			tips_label_2->SetQStringText(tips2);
	}
}

// 设置选中
void QSettingButtonBase::SetChecked()
{
	tips_label_1->SetChecked();
}

// 移除选中
void QSettingButtonBase::RemoveChecked()
{
	tips_label_1->RemoveChecked();
}

// 设置此控件是几号数字
void QSettingButtonBase::setNumber(Q_KeyNumber key)
{
	m_keyNumber = key;
}

//窗口大小发生变化自动调用虚函数
void QSettingButtonBase::resizeEvent(QResizeEvent *event)
{
//	m_pQWidget->move(0,0);
////	m_pQWidget->resize(width(),height());
//	m_pQWidget->setMaximumSize(width(),height());
//	m_pQWidget->setMinimumSize(width(),height());


    if(tips_label_2){
    	tips_label_1->setMaximumSize(width() * 11 / 20,height()/2);
        tips_label_1->setMinimumSize(0,0);
    	tips_label_2->setMaximumSize(width() * 11 / 20,height()-2);
        tips_label_2->setMinimumSize(0,0);
    }else{

    	tips_label_1->setMaximumSize(width() * 5 / 6,height());
        tips_label_1->setMinimumSize(width() * 4 / 6 - 20,0);
    }

    if(icon_label){
    	icon_label->setMaximumSize(width() * 1 / 6,height()-2);
		icon_label->setMinimumSize(width() * 1 / 6,0);
    }


    if(num_icon_label){
    	num_icon_label->setMaximumSize(width() * 1 / 6,height()-2);
        num_icon_label->setMinimumSize(width() * 1 / 6,0);
    }
	
}

void QSettingButtonBase::paintEvent(QPaintEvent *event)
{
	QWidgetBase::paintEvent(event);
	Q_UNUSED(event);

	QStyleOption opt;
	opt.init(this);
	QPainter p(this);
	style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
	if(m_IsDrawRectangle)
		QUnits::drawectanglRe(&p,0,0,width(),height(), QColor(0,255,255));
	if(m_upSize > 0){
		m_upSize--;
		update();
	}
}

bool QSettingButtonBase::event(QEvent *event)
{

    switch(event->type())
    {
    case QEvent::TouchBegin:
        {
            event->accept();
            m_IsDrawRectangle = true;
            this->update();
            qDebug() << "QEvent::TouchBegin -> 触摸按下   ";
            return true;

        }

    case QEvent::TouchUpdate:
        {
            event->accept();
            qDebug() << "QEvent::TouchUpdate -> 触摸保持  ";
            return true;
        }
    case QEvent::TouchEnd:			//触摸松开后
        {
            event->accept();
            m_IsDrawRectangle = false;
            this->update();
			// 发送键盘按下消息
			QKeyEvent keyPress(QEvent::KeyPress,    Qt::Key_0 + m_keyNumber, Qt::NoModifier);
			QWidget *receiver = QApplication::focusWidget();
			QApplication::sendEvent(receiver, &keyPress);
			// 发送键盘松开消息
			QKeyEvent keyRelease(QEvent::KeyRelease,    Qt::Key_0 + m_keyNumber, Qt::NoModifier);
			QApplication::sendEvent(receiver, &keyRelease);
            qDebug() << "QEvent::TouchEnd -> 触摸松开后  ";
            return true;
        }
    case QEvent::MouseButtonPress:	// 鼠标按下
		{

			QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
			if(mouseEvent->button() == Qt::LeftButton){
	            event->accept();
	            m_IsDrawRectangle = true;
	            this->update();
	            qDebug() << "QEvent::MouseButtonPress -> 鼠标按下   ";
	            return true;
			}
			break;
		}
    case QEvent::MouseButtonRelease:	// 鼠标释放
		{

			QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
			if(mouseEvent->button() == Qt::LeftButton){
				event->accept();
				m_IsDrawRectangle = false;
				this->update();
				// 发送键盘按下消息
				QKeyEvent keyPress(QEvent::KeyPress,    Qt::Key_0 + m_keyNumber, Qt::NoModifier);
				QWidget *receiver = QApplication::focusWidget();
				QApplication::sendEvent(receiver, &keyPress);
				// 发送键盘松开消息
				QKeyEvent keyRelease(QEvent::KeyRelease,    Qt::Key_0 + m_keyNumber, Qt::NoModifier);
				QApplication::sendEvent(receiver, &keyRelease);
				qDebug() << "QEvent::MouseButtonRelease -> 鼠标释放 ";
				return true;
			}
			break;
		}
    default:
    	break;
    }
    return QWidgetBase::event(event);
}

